import pygame, os.path, math

class shell:
    def __init__(self, game, shooter, target, speed, angle):
        self.game = game
        self.game_logic = self.game.game_logic
        self.shooter = shooter
        
        self.image = pygame.Surface((4, 4))
        self.image.fill((0, 0, 0))
        self.rect = self.image.get_rect()
        
        self.target = target
        self.rect.center = shooter.rect.center
        self.speed = 15
        self.fire_speed = speed
        self.fire_angle = angle
        self.velocity_adjustX = math.cos(math.radians(self.fire_angle)) * float(self.fire_speed)
        self.velocity_adjustY = math.sin(math.radians(self.fire_angle)) * float(self.fire_speed)
        
        #find real target after adjusting for fire velocity
        tX, tY = self.target
        sX, sY = self.rect.center
        diffX = tX - sX
        diffY = tY - sY
        dist = math.sqrt(diffX**2 + diffY**2)
        iters_to_target = dist / float(self.speed)
        tX+= (self.velocity_adjustX * iters_to_target)
        tY-= (self.velocity_adjustY * iters_to_target)
        self.target = (tX, tY)
        tX, tY = self.target
        sX, sY = self.rect.center
        diffX = tX - sX
        diffY = tY - sY
        dist = math.sqrt(diffX**2 + diffY**2)
        self.perFrameX = diffX / iters_to_target
        self.perFrameY = diffY / iters_to_target
        
        
        self.alive = True
        self.leftoverX = 0
        self.leftoverY = 0
    
    def update(self):
        if self.alive:
            tX, tY = self.target
            sX, sY = self.rect.center
            diffX = tX - sX
            diffY = tY - sY
            dist = math.sqrt(diffX**2 + diffY**2)
            if dist < self.speed:
                self.rect.center = self.target
                dist = 0
            if not dist == 0:
                changeX = self.perFrameX
                changeY = self.perFrameY
                loX = changeX % 1
                loY = changeY % 1
                self.leftoverX+= loX
                self.leftoverY+= loY
                changeX-= loX
                changeY-= loY
                if self.leftoverX > 1:
                    changeX+= self.leftoverX
                    self.leftoverX = self.leftoverX % 1
                if self.leftoverY > 1:
                    changeY+= self.leftoverY
                    self.leftoverY = self.leftoverY % 1
                self.rect.x+= changeX
                self.rect.y+= changeY
            else:
                self.alive = False
        
        if self.game.config.host and self.alive:
            self._test_hits()
        
        if self.alive:
            self._test_colliders()
        
        if not self.alive:
            self.rect.center = (-2, -2)
    
    def _test_hits(self):
        for i in range(len(self.game_logic.tank_list)):
            tank = self.game_logic.tank_list[i]
            if self.rect.colliderect(tank.rect) and not tank == self.shooter:
                self.alive = False
                misc = {"target": tank.player.player_id}
                self.game_logic.send_user_event("tank_hit", self.shooter.player, misc)
    
    def _test_colliders(self):
        for collider in self.game_logic.colliders:
            if self.rect.colliderect(collider.rect) and not collider == self.shooter:
                self.alive = False
